package com.harmonyos.samples.utils;

import com.harmonyos.samples.bean.code.LogInfo;
import com.harmonyos.samples.constants.Constants;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.components.JBList;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;

import javax.swing.*;
import java.awt.*;

public class ToolWindowController {

    private ToolWindow toolWindow;

    private JBList<LogInfo> jbList;

    private DefaultListModel<LogInfo> defaultListModel = new DefaultListModel<>();

    public ToolWindowController(Project project) {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        toolWindow = toolWindowManager.getToolWindow(Constants.PLUGIN_TITLE);
        if (toolWindow == null) {
            toolWindow = toolWindowManager.registerToolWindow(Constants.PLUGIN_TITLE, false, ToolWindowAnchor.BOTTOM);
        }
    }

    public void addTab(String title) {
        Content content = ContentFactory.getInstance().createContent(createToolWindowComponent(), title, false);
        toolWindow.getContentManager().addContent(content);
    }

    private JPanel createToolWindowComponent() {
        JPanel jPanel = new JPanel(new BorderLayout());
        jPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1, Color.GRAY));

        jbList = new JBList<>(defaultListModel);
        jbList.setAlignmentX(0);
        jbList.setAlignmentY(0);
        jbList.setCellRenderer((jList, logInfo, index, selected, cellHasFocus)-> {
            FlowLayout flowLayout = new FlowLayout();
            flowLayout.setAlignment(FlowLayout.LEFT);

            JPanel itemPanel = new JPanel(flowLayout);
            itemPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 0, 0, Color.GRAY));
            JLabel library = new JLabel();
            library.setPreferredSize(new Dimension(200, 30));
            library.setText(logInfo.getLibraryName());
            itemPanel.add(library);

            JLabel fileName = new JLabel();
            fileName.setPreferredSize(new Dimension(200, 30));
            fileName.setText(logInfo.getFileName());
            itemPanel.add(fileName);

            JLabel methodName = new JLabel();
            methodName.setPreferredSize(new Dimension(200, 30));
            methodName.setText(logInfo.getMethodName());
            itemPanel.add(methodName);

            JTextPane code = new JTextPane();
            code.setEditable(false);
            code.setText(logInfo.getCode());
            itemPanel.add(code);

            return itemPanel;
        });

        LogInfo titleInfo = new LogInfo();
        titleInfo.setLibraryName("仓库名");
        titleInfo.setCode("写入代码");
        titleInfo.setMethodName("方法名");
        titleInfo.setFileName("文件名");
        update(titleInfo);
        JBScrollPane jbScrollPane = new JBScrollPane(jbList);
        jbScrollPane.setPreferredSize(new Dimension(2000, 800));
        jPanel.add(jbScrollPane, BorderLayout.CENTER);
        return jPanel;
    }

    public void update(LogInfo logInfo) {
        defaultListModel.add(defaultListModel.size(), logInfo);
        jbList.setModel(defaultListModel);
    }

    public void show() {
        if (toolWindow != null) {
            toolWindow.show();
        }
    }
}
